package com.example.templateapp.ui.recycler

import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.example.templateapp.R
import com.example.templateapp.databinding.ActivityRecyclerViewBinding
import com.example.templateapp.ui.adapter.base.SimpleDelegateAdapter
import com.google.android.material.chip.Chip
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder
import com.xuexiang.xutil.common.ClickUtils
import com.xuexiang.xutil.tip.ToastUtils

class RecyclerViewActivity : AppCompatActivity() {

    private lateinit var binding: ActivityRecyclerViewBinding
    private var isScrolledToTop = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recycler_view)

        val list = mutableListOf<String>()
        for (i in 1..25) {
            list.add("$i")
        }

        binding.rv.adapter = object :
            SimpleDelegateAdapter<String>(
                R.layout.adapter_item_tv_match,
                LinearLayoutHelper(),
                list
            ) {
            override fun bindData(holder: RecyclerViewHolder, position: Int, item: String) {
                holder.text(R.id.tv_item, item)
            }
        }

        binding.rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                isScrolledToTop = false
                val layoutManager: LinearLayoutManager =
                    binding.rv.layoutManager as LinearLayoutManager
                val firstVisibleItemPosition =
                    layoutManager.findFirstCompletelyVisibleItemPosition()

                Log.d("binding.rv", "scrolling")
                if (firstVisibleItemPosition == 0) {
                    isScrolledToTop = true
                    Log.d("binding.rv", "scroll to top")
                }
                val lastVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition()
                if (lastVisibleItemPosition == layoutManager.itemCount - 1) {
                    Log.d("binding.rv", "scroll to bottom")
                }

            }
        })
        val actionStyle = R.style.Widget_MaterialComponents_Chip_Choice
        for (i in 1..10) {

            val chip = Chip(this)
            chip.isCheckable = true
            chip.isCheckable = true
            chip.isCheckedIconVisible = true

            var txt = ""
            for (j in 0..i) {
                txt += i.toString()
            }
            chip.setText("$txt")
            binding.chipGroup.addView(chip)
        }

        binding.entryChip.setOnCloseIconClickListener {
            binding.chipGroup.removeView(it)
        }

        binding.chipGroup.setOnCheckedChangeListener { g, i ->
            ToastUtils.toast("g=$g,i=$i")
        }


        binding.chipGroup.setOnClickListener {

            val checkedIds = binding.chipGroup.checkedChipIds
            ToastUtils.toast("checkedIds=$checkedIds")
        }


    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (!isScrolledToTop) {
//                binding.rv.scrollToPosition(0)
                binding.rv.smoothScrollToPosition(0)
                return true
            }

            ClickUtils.exitBy2Click(2000, object : ClickUtils.OnClick2ExitListener {
                override fun onRetry() {
                    ToastUtils.toast("再按一次退出程序", 500)
                }

                override fun onExit() {
                    finish()
                }
            })
        }
        return true

    }

}